package com.fr.base.core.util;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Ascii85InputStream extends FilterInputStream
{
  private int index = this.n = 0;
  private int n;
  private boolean eof = false;
  private byte[] ascii = new byte[5];
  private byte[] b = new byte[4];

  public Ascii85InputStream(InputStream paramInputStream)
  {
    super(paramInputStream);
  }

  public final int read()
    throws IOException
  {
    int i;
    long l;
    if (this.index >= this.n)
    {
      int j;
      int k;
      if (this.eof)
        return -1;
      this.index = 0;
      do
      {
        k = (byte)this.in.read();
        if (k == -1)
        {
          this.eof = true;
          return -1;
        }
        j = (byte)k;
      }
      while ((j == 10) || (j == 13) || (j == 32));
      if ((((j == 126) ? 1 : 0) | ((j == 120) ? 1 : 0)) != 0)
      {
        this.eof = true;
        this.ascii = (this.b = null);
        this.n = 0;
        return -1;
      }
      if (j == 122)
      {
        this.b[0] = (this.b[1] = this.b[2] = this.b[3] = 0);
        this.n = 4;
      }
      else
      {
        this.ascii[0] = j;
        for (i = 1; i < 5; ++i)
        {
          do
          {
            k = (byte)this.in.read();
            if (k == -1)
            {
              this.eof = true;
              return -1;
            }
            j = (byte)k;
          }
          while ((j == 10) || (j == 13) || (j == 32));
          this.ascii[i] = j;
          if ((((j == 126) ? 1 : 0) | ((j == 120) ? 1 : 0)) != 0)
            break;
        }
        this.n = (i - 1);
        if (this.n == 0)
        {
          this.eof = true;
          this.ascii = (this.b = null);
          return -1;
        }
        if (i < 5)
        {
          ++i;
          while (i < 5)
          {
            this.ascii[i] = 33;
            ++i;
          }
          this.eof = true;
        }
        l = 0L;
        for (i = 0; i < 5; ++i)
        {
          j = (byte)(this.ascii[i] - 33);
          if ((j < 0) || (j > 93))
          {
            this.n = 0;
            this.eof = true;
            this.ascii = (this.b = null);
            throw new IOException("Invalid data in Ascii85 stream");
          }
          l = l * 85L + j;
        }
        for (i = 3; i >= 0; --i)
        {
          this.b[i] = (byte)(int)(l & 0xFF);
          l >>>= 8;
        }
      }
    }
    return (this.b[(this.index++)] & 0xFF);
  }

  public final int read(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
    throws IOException
  {
    if ((this.eof) && (this.index >= this.n))
      return -1;
    for (int i = 0; i < paramInt2; ++i)
      if (this.index < this.n)
      {
        paramArrayOfByte[(i + paramInt1)] = this.b[(this.index++)];
      }
      else
      {
        int j = read();
        if (j == -1)
          return i;
        paramArrayOfByte[(i + paramInt1)] = (byte)j;
      }
    return paramInt2;
  }

  public void close()
    throws IOException
  {
    this.ascii = null;
    this.eof = true;
    this.b = null;
    super.close();
  }

  public boolean markSupported()
  {
    return false;
  }

  public long skip(long paramLong)
  {
    return 0L;
  }

  public int available()
  {
    return 0;
  }

  public void mark(int paramInt)
  {
  }

  public void reset()
    throws IOException
  {
    throw new IOException("Reset is not supported");
  }
}